package com.laker.autotest.dev;

import cn.hutool.core.collection.CollUtil;
import com.laker.autotest.utils.PlaywrightUtil;
import com.microsoft.playwright.*;

import java.nio.file.Paths;

/**
 * <p>
 * 脚本录制
 * 如果复用登录信息则启用
 * BrowserContext context = browser.newContext(new Browser.NewContextOptions()
 * // 加载cookie和localstorage, 模拟登录态
 * .setStorageStatePath(Paths.get("auth.json")));
 * 设置录制时窗口最大化，默认是1280x720
 * 1.BrowserType.LaunchOptions()  .setArgs(CollUtil.newArrayList("--start-maximized"))
 * 2.BrowserType.LaunchOptions()  .setHeadless(false)
 * 3.new Browser.NewContextOptions() .setViewportSize(null)
 * </p>
 */
public class Record {
    public static void main(String[] args) {
        PlaywrightUtil.installBrowser("chromium");
        try (Playwright playwright = PlaywrightUtil.initPlaywright()) {
            BrowserType chromium = playwright.chromium();
            // Make sure to run headed.
            Browser browser = chromium.launch(new BrowserType.LaunchOptions()
                    .setArgs(CollUtil.newArrayList("--start-maximized"))
                    // 开启开发者模式
                    // 可以在开发者控制台使用 playwright对象中的任何方法调试，例如常见的元素定位
                    .setDevtools(true)
                    .setHeadless(false));
            // Setup context however you like.
            BrowserContext context = browser.newContext(new Browser.NewContextOptions()
                    .setViewportSize(null));
            context.route("**/*", Route::resume);
            // Pause the page, and start recording manually.
            Page page = context.newPage();
            page.pause();
            // 把登录状态，即浏览器的cookie和localstorage存储到文件中。
            context.storageState(new BrowserContext.StorageStateOptions().setPath(Paths.get("auth.json")));
        }
    }
}
